﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LumenWorks.Framework.IO.Csv;
using System.IO;
using System.Globalization;

namespace Utilities
{
    public class Report
    {
        public string strReportContentBeingBuilt = "";

        public Report()
        {
            strReportContentBeingBuilt = ReportUtil.loadReportTemplateFromCSV(Utilities.GlobalFile.REPORT_TEMPLATE_FILENAME);
            setStartTime();
        }

        public void endReport()
        {
            setEndTime();
            calculateProgressAndResult();
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_TEMPLATE", "");
            ReportUtil.savestrReportContentBeingBuilt2File(Utilities.GlobalFile.REPORT_FILENAME, strReportContentBeingBuilt);

            ////Set to mail
            Utilities.GlobalObject.MAIL.setBody(strReportContentBeingBuilt);
        }

        public void createNewTestCaseHeader()
        {
            string newHeader = "", newTestCase = "";
            string rowStart = "<tr>", rowEnd = "</tr>";
            string cellStartheader = "<td><font color=BLUE><b>";
            string cellStart = "<td bgcolor=BLUE><font color=WHITE><b>";
            string cellEnd = "</b></font></td>";

            newTestCase = rowStart + cellStartheader + "Test Case " + Utilities.GlobalTestStep.intTestCaseOrder + cellEnd +
                            cellStartheader + cellEnd +
                            cellStartheader + cellEnd +
                            cellStartheader + cellEnd +
                            cellStartheader + cellEnd +
                            rowEnd;
            newHeader += rowStart +
                            cellStart + "Owner" + cellEnd +
                            cellStart + "Action" + cellEnd +
                            cellStart + "Result" + cellEnd +
                            cellStart + "Test Data" + cellEnd +
                            cellStart + "Validation Text Present" + cellEnd +
                         rowEnd;
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_TEMPLATE", newTestCase + newHeader + "SM2_REPORT_TEMPLATE");
        }

        public void createNewTestStepItemPass(string strTestData)
        {
            string newRow = "";
            string rowStart = "<tr>", rowEnd = "</tr>";
            string cellStart = "<td>", cellStartPass = "<td>", cellEnd = "</td>";
            string strVal = "";
            if (Utilities.GlobalTestStep.strValidation.Equals(""))
                strVal = "\"" + Utilities.GlobalTestStep.strValidation + "\" : N/A";
            else
                strVal = "\"" + Utilities.GlobalTestStep.strValidation + "\" : Pass";
            //Prepare html format
            newRow += rowStart +
                            cellStart + Utilities.GlobalTestStep.strOwner + cellEnd +
                            cellStart + Utilities.GlobalTestStep.strAction + cellEnd +
                            cellStartPass + "Passed" + cellEnd +
                            cellStartPass + strTestData + cellEnd +
                            cellStartPass + strVal + cellEnd +
                      rowEnd;

            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_TEMPLATE", newRow + "SM2_REPORT_TEMPLATE");
        }

        public void createNewTestStepItemFail(string strTestData)
        {
            string newRow = "";
            string rowStart = "<tr>", rowEnd = "</tr>";
            string cellStart = "<td>", cellStartFail = "<td bgcolor=RED>", cellEnd = "</td>";

            //Prepare html format
            newRow += rowStart +
                            cellStart + Utilities.GlobalTestStep.strOwner + cellEnd +
                            cellStart + Utilities.GlobalTestStep.strAction + cellEnd +
                            cellStartFail + "Failed" + cellEnd +
                            cellStart + strTestData + cellEnd +
                            cellStart + "\"" + Utilities.GlobalTestStep.strValidation + "\" : N/A" + cellEnd +
                      rowEnd;

            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_TEMPLATE", newRow + "SM2_REPORT_TEMPLATE");
        }

        public void createNewTestStepItemFailByValidtion(string strTestData)
        {
            string newRow = "";
            string rowStart = "<tr>", rowEnd = "</tr>";
            string cellStart = "<td>", cellStartFail = "<td bgcolor=RED>", cellEnd = "</td>";

            //Prepare html format
            newRow += rowStart +
                            cellStart + Utilities.GlobalTestStep.strOwner + cellEnd +
                            cellStart + Utilities.GlobalTestStep.strAction + cellEnd +
                            cellStart + "N/A" + cellEnd +
                            cellStart + strTestData + cellEnd +
                            cellStartFail + "\"" + Utilities.GlobalTestStep.strValidation + "\" : Fail" + cellEnd +
                      rowEnd;

            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_TEMPLATE", newRow + "SM2_REPORT_TEMPLATE");
        }

        private void setStartTime()
        {
            string strStartTime = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + " " +
                                DateTime.Now.Day.ToString() + "-" + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_START_TIME", strStartTime);
        }

        private void setEndTime()
        {
            string strEndTime = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + " " +
                                DateTime.Now.Day.ToString() + "-" + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_END_TIME", strEndTime);
        }

        private void calculateProgressAndResult()
        {
            //Calculate progress
            double dblTotalTestCase = (double)Utilities.GlobalTestStep.intTotalTestCase;
            double dblNumberTestCaseHaveBeenExecuted = (double)Utilities.GlobalTestStep.intTestCaseOrder;
            double dblProgressFinal = Math.Ceiling((dblNumberTestCaseHaveBeenExecuted / dblTotalTestCase) * 100);
            string strProgress = dblProgressFinal.ToString() + "% Completed";
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_OK_PERCETAGE", strProgress);

            //Calculate result
            string strMailContent = strReportContentBeingBuilt;
            double dblCheckPointPass = ReportUtil.countWord(strMailContent, "Passed");
            double dblCheckPointFail = ReportUtil.countWord(strMailContent, "Failed");
            double dblResultFinal = Math.Ceiling((dblCheckPointPass / (dblCheckPointFail + dblCheckPointPass)) * 100);
            string strResult = dblResultFinal.ToString() + "%";
            strReportContentBeingBuilt = strReportContentBeingBuilt.Replace("SM2_REPORT_NOTOK_PERCENTAGE", strResult);
        }
    }

    public class ReportUtil
    {
        public static string loadReportTemplateFromCSV(string strReportTemplateFileDirectory)
        {
            string strReportTemplateContent = "";
            try
            {
                CsvReader csv_report_template = new CsvReader(new System.IO.StreamReader(strReportTemplateFileDirectory), true);
                string[] headers_csv_report_template = csv_report_template.GetFieldHeaders();
                int fieldCounts_csv_report_template = csv_report_template.FieldCount;
                string newLine = "\r\n";
                while (csv_report_template.ReadNextRecord())
                {
                    strReportTemplateContent += csv_report_template[0] + newLine;
                }
                csv_report_template.Dispose();
            }
            catch { }
            return strReportTemplateContent;
        }

        public static void savestrReportContentBeingBuilt2File(string strReportFileName, string strReportContentBeingBuilt)
        {
            FileUtil.saveFile(strReportFileName, strReportContentBeingBuilt);
        }

        public static double countWord(string paragraph, string keyword)
        {
            double counter = 0;
            int lengthKeyWord = keyword.Length;
            for (int i = 0; i < (paragraph.Length - lengthKeyWord); i++)
            {
                string strTemp = paragraph.Substring(i, lengthKeyWord);
                if (strTemp.ToLower().Equals(keyword.ToLower())) counter++;
            }

            return counter;
        }
    }
}
